﻿Imports sonicERPDAL
Imports sonicERPinfo
Imports System.Globalization
Imports System.DateTime

Partial Public Class NivelLista
    Inherits System.Web.UI.Page
    Dim dal As New nivelUsuarioDAL()
    Dim util As New Util
  
    

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
          
            Dim paginavisivel As Boolean
            paginavisivel = util.validarpagina(2)
            If Not paginavisivel Then
                FormsAuthentication.SignOut()
                Response.Redirect(ResolveUrl("~/Login.aspx"))
            End If
            getnivelusuario()
        End If
    End Sub



    Private Sub gvList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvList.PageIndexChanging
        e.Cancel = True
        gvList.PageIndex = e.NewPageIndex
        getnivelusuario()

    End Sub






    Protected Sub gvList_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles gvList.SelectedIndexChanging
        Dim id As String = gvList.DataKeys(e.NewSelectedIndex).Value.ToString
        Response.Redirect("~/NomeDaMinhaPagina.aspx?id=" + id)

    End Sub

    Protected Sub gvList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvList.SelectedIndexChanged

    End Sub

    Public Sub getnivelusuario()
        Try
            pnl1.Visible = False
            Dim modelo As New NivelUsuarioInfo
            


            If String.IsNullOrEmpty(txtnivel.Text) Then
                modelo.nivel = Convert.ToString(DBNull.Value)
            Else
                modelo.nivel = txtnivel.Text
            End If
            If txtdataininico.Text = "" Then
                Dim data As DateTime = DateTime.Today.AddMonths(-1)
                txtdataininico.Text = Convert.ToString(data.ToShortDateString)
                modelo.dtinicio = Convert.ToDateTime(txtdataininico.Text)
                txtdataininico.Text = FormataData(txtdataininico.Text)
            Else
                Dim culture As New System.Globalization.CultureInfo("pt-BR")
                Dim dt As String = Convert.ToDateTime(txtdataininico.Text).ToString("yyyy/MM/dd HH:mm:ss", culture)
                modelo.dtinicio = Convert.ToDateTime(dt).ToString("yyyy/MM/dd HH:mm:ss", culture)
            End If


            If txtdatafim.Text = "" Then
                Dim data As DateTime = DateTime.Today
                txtdatafim.Text = Convert.ToString(data.ToShortDateString)
                modelo.dt_fim = Convert.ToDateTime(txtdatafim.Text)
                txtdatafim.Text = FormataData(txtdatafim.Text)
            Else
                Dim culture As New System.Globalization.CultureInfo("pt-BR")
                Dim dt As String = Convert.ToDateTime(txtdatafim.Text).ToString("yyyy/MM/dd HH:mm:ss", culture)
                modelo.dt_fim = Convert.ToDateTime(dt).ToString("yyyy/MM/dd HH:mm:ss", culture)
            End If

            gvList.DataSource = dal.lista(modelo, True)
            gvList.DataBind()
            lblerro.Text = ""
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try





    End Sub

    Public Function FormataData(ByVal Data)
        If Data <> "" Then
            Dim dt As DateTime = Data
            Data = dt.ToString("dd/MM/yyyy")

        End If
        Return Data
    End Function

    '' Public Function datainicio()
    '' Dim data As DateTime = DateTime.Now
    '' Dim brasil As New CultureInfo("pt-BR")
    '' Dim dia As String = "01"
    '' Dim hoje As String


    ''     data = DateAdd(DateInterval.Month, -1, Now)
    ''  Dim dataini As DateTime
    ''    hoje = Convert.ToString(dia & "/" & data.Month & "/" & data.Year)
    ''   dataini = Convert.ToDateTime(hoje)
    ''   Return dataini




    ''End Function
    ' Public Function datafim()
    ''  Dim dia As Integer
    ''  Dim hoje As String
    ''  Dim data As DateTime = DateTime.Now
    ''  Dim brasil As New CultureInfo("pt-BR")
    ''     data = DateAdd(DateInterval.Month, 1, Now)
    '' Dim datafi As DateTime
    ''   dia = DateTime.DaysInMonth(data.Year, data.Month)
    ''  hoje = Convert.ToString(dia & "/" & data.Month & "/" & data.Year)
    ''  datafi = Convert.ToDateTime(hoje)
    ''  Return datafi.ToString(brasil)

    '' End Function







    Protected Sub btnok_Click1(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnok.Click
        Try
            pnl1.Visible = False
            If txtdataininico.Text = "" Or txtdatafim.Text = "" Then
                txtdataininico.BackColor = Drawing.Color.White
                txtdatafim.BackColor = Drawing.Color.White
                getnivelusuario()
                lblerro.Text = ""
                Return
            Else

                Dim di As DateTime
                DateTime.TryParseExact(txtdataininico.Text, "dd/MM/yyyy", Nothing, Nothing, di)
                Dim df As DateTime
                DateTime.TryParseExact(txtdatafim.Text, "dd/MM/yyyy", Nothing, Nothing, df)


                If di = DateTime.MinValue Then
                    txtdataininico.Focus()
                    txtdataininico.BackColor = Drawing.Color.Aqua
                    txtdatafim.BackColor = Drawing.Color.White
                    Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                Else
                    txtdataininico.BackColor = Drawing.Color.White
                    If df = DateTime.MinValue Then
                        txtdatafim.Focus()
                        txtdatafim.BackColor = Drawing.Color.Aqua
                        txtdataininico.BackColor = Drawing.Color.White
                        Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                    Else
                        txtdatafim.BackColor = Drawing.Color.White
                    End If
                End If
            End If
            getnivelusuario()
            lblerro.Text = ""
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub

    Protected Sub gvList_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvList.RowCommand
        If e.CommandName = "Editar" Then
            Dim i As Integer
            i = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = gvList.Rows(i)


            Dim url As String
            url = String.Format("~/adm/Cadastronivel.aspx?id=" + Server.HtmlDecode(gvList.DataKeys(i).Value) + "&nivel=" + Server.HtmlDecode(row.Cells(2).Text))
            Response.Redirect(ResolveUrl(url))
        End If
    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim url As String = String.Format("~/adm/Cadastronivel.aspx")
        Response.Redirect(ResolveUrl(url))
    End Sub

   
    Protected Sub btnimprimir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnimprimir.Click
        Dim javascript As String = "<script language ='javascript'>"
        javascript += " window.open('Imprimir.aspx?idrelatorio=3&nome=" + Server.HtmlDecode(txtnivel.Text) + "&dtinicio=" + Server.HtmlDecode(txtdataininico.Text) + "" +
      "&dtfim=" + Server.HtmlDecode(txtdatafim.Text) + " ', '', 'letf=0,top=0,width=900px,height=900px,toolbar=0,scrollbars=1,status=0');"
        javascript += " </script>"
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "imprimir", javascript)

    End Sub
End Class